<template>
  <div>
    <!-- list -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <div class="cmt-box">
          <van-image fit="cover" round :src="item.aut_photo"></van-image>
          <!-- 中间核心区域 -->
          <div class="center-box">
            <div class="name">{{ item.aut_name }}</div>
            <div class="content">{{ item.content }}</div>
            <div class="time">
              <span>{{ $relvTime(item.pubdate) }}</span>
              <van-button @click="doReply(item)" size="mini">回复({{ item.reply_count }})</van-button>
            </div>
          </div>
          <van-icon name="like-o" />
        </div>
      </van-cell>
    </van-list>
    <!-- 放固定定位的div，里面放输入框和按钮 -->
    <div class="send-box">
      <van-field
        v-model="cmt"
        center
        clearable
        placeholder="请输入评论内容"
        @keyup.enter="send"
      >
        <template #button>
          <van-button @click="send" size="small" type="info">发布</van-button>
        </template>
      </van-field>
    </div>

    <!-- 回复区域 -->
    <van-action-sheet v-model="replyShow" title="发表回复">
      <ReplyComment v-if="replyShow" :currentItem="currentItem" @add="currentItem.reply_count++"/>
    </van-action-sheet>
  </div>
</template>

<script>
// 导入接口
import { cmtListAPI, sendCmtAPI } from '@/api/comment'
// 导入回复的组件
import ReplyComment from './ReplyComment.vue'
export default {
  components: {
    ReplyComment
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      cmt: '',
      offset: null,
      replyShow: false,
      currentItem: null
    }
  },
  methods: {
    // 点击回复触发的事件
    doReply (item) {
      // 1. 显示出sheet
      this.replyShow = true
      // 2. 把当前被点的评论传给回复里去显示
      this.currentItem = item
    },
    // 发表评论
    async send () {
      if (this.cmt === '') {
        return this.$toast('请输入评论内容')
      }
      // 能来到这肯定不为空
      const res = await sendCmtAPI({
        // 如果是评论，传文章id
        target: this.$route.params.id,
        // 评论的内容
        content: this.cmt
      })

      // 清空输入内容
      this.cmt = ''
      // 把服务器返回的最新的这条评论直接加到数组里
      this.list.unshift(res.data.new_obj)
    },
    async onLoad () {
      const res = await cmtListAPI({
        type: 'a',
        source: this.$route.params.id,
        offset: this.offset,
        limit: 5
      })

      this.list.push(...res.data.results)
      // 把本次的最后一个id存起来
      this.offset = res.data.last_id
      // 加载本次加载状态
      this.loading = false
      // 判断数组的长度大于或等于服务器告诉的总数量就代表加载完了
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep {
  .van-list {
    margin-bottom: 52px;
  }
  .send-box {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100vw;
  }

  .cmt-box {
    display: flex;
    .van-image {
      width: 40px;
      height: 40px;
    }

    .center-box {
      flex: 1;
      margin-left: 15px;
      .name {
        color: #466b9d;
      }
      .content {
        margin: 10px 0;
      }

      .time {
        color: gray;
        font-size: 12px;
        display: flex;
        align-items: center;

        .van-button {
          margin-left: 5px;
        }
      }
    }
  }
}
</style>
